#include "cmd-options.h"


namespace {

namespace options {

using Opt = CmdOptions::Option;

static const Opt help{"help", 'h',
	"Show this message"};

static const Opt logLevel{"loglevel", 'L',
	"Log level for default logger, "
	"available values: a(all),d(debug),i(info),e(error)" };




}

}

CmdOptions::CmdOptions(int argc, char *argv[])
{
	namespace po = boost::program_options;

	if (argc <= 0) {
		throw std::logic_error("CmdOptions::CmdOptions() Bad argc");
	}

	exe_ = fs::absolute(argv[0]);

	po::options_description_easy_init add = desc_.add_options();
	OptionsDescEasyInit addWrapper(add);
	addOptions(addWrapper);

	
	add("config,c", po::value<std::vector<std::string>>(), "configuration name and value pairs, format: [name:value]");

	std::vector<std::string> posArgs;
	const char *cstrConfigFile = "config-file";
	add(cstrConfigFile, po::value<std::vector<std::string>>(&posArgs), "configuration file path");
	posdesc_.add(cstrConfigFile, -1);

	po::store(po::command_line_parser(argc, argv).options(desc_).positional(posdesc_).run(), varmap_);
	po::notify(varmap_);
}

void CmdOptions::addOptions(OptionsDescEasyInit &add)
{
	add(options::help);
	add(options::logLevel);
}

bool CmdOptions::needHelp() const
{
	return varmap_.count(options::help.longName()) > 0;
}

void CmdOptions::printHelp(std::ostream &stream)
{
	const std::string tab = "    ";
	const std::string app = exe_.filename().generic_string();

	stream << "Usage: " << std::endl;
	stream << tab << app << " [Options] [configuration file(s)]" << std::endl;
	stream << std::endl;
	stream << desc_ << std::endl;
	stream << std::endl;
}

std::string CmdOptions::valueOfLogLevel() const
{
	return "a"; // TODO
}
